<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>聊天</title>
</head>
<body>
<button onclick="send_msg()">
    发个消息试一下
</button>

<script>
    let ws = new WebSocket("ws://localhost:8080/ws/" + Math.random());
    ws.onopen = function (evt) {
        alert("建立连接");
    };

    ws.onmessage = function (evt) {
        alert(evt.data);

    };

    ws.onerror = function (evt) {
        alert(evt.data);
    };

    ws.onclose = function (evt) {
        alert(evt.data);
    };
    function send_msg(){
        ws.send('发个消息试一下！！');
    }

</script>
</body>
</html>